/* Copyright(C): Leaf Johnson 2009,2010

    This file is part of leaf-lib.

    leaf-lib is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    leaf-lib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with leaf-lib.  If not, see <http://www.gnu.org/licenses/>.


 * 
 * 
 */


#ifndef SYSCLK_H_INCLUDED
#define SYSCLK_H_INCLUDED
#include <leaf/osiface.h>
/* 使用单片机的第一个T/C设备，做为系统的时钟节拍源 */

/* 初始化设备，具体数据用户自行计算 */
void sysclk_init( uint8_t tccr, unsigned div, unsigned tickRate );
inline unsigned getHz( void );
#define MS_TICKS( m ) ( ((uint32_t)(m) * getHz() ) / 1000 )
#define TICKS_MS( t ) ( ((uint32_t)(t) * 1000 ) / getHz() )
static inline void sleep_ms( pTimer tmr, unsigned ms )
{
	sleep_ticks( tmr, MS_TICKS( ms ) );
}
uint32_t get_running_ms( void );
uint32_t get_jaffies( void );
#define TICKRATE_TCNT( tick, div ) \
	( 0xFF - F_CPU / tick / div )

/* 时钟源分频设置，请注意与自己的MCU相匹配 */
#define SYSCLK_CK1024 ( ( _BV(CS00) | _BV(CS01) )｜_BV( CS02 ) )
#define SYSCLK_CK256  ( _BV(CS01) | _BV(CS02) )
#define SYSCLK_CK128  ( _BV(CS00) | _BV(CS02) )
#define SYSCLK_CK64   ( _BV(CS02) )

#endif /* OSCLK_H_INCLUDED */
